﻿/******************************************************/
// OrderPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using MAA.Business;
using MAA.Entity;

namespace MAA.Presentation.PRL.Trading
{
    public partial class OrderPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            hplPostDetail.Text = rm.GetString("langSeeDetail");
            langOrderInformation.Text = rm.GetString("langOrderInformation");
            langAmount.Text = rm.GetString("langAmountNotKg");
            langUnitPrice.Text = rm.GetString("langUnitPrice");
            langDateBuy.Text = rm.GetString("langDateBuy");
            langStatus.Text = rm.GetString("langStatus");
            btnCancelOrder.Text = rm.GetString("langCancelOrder");
            langThisPostInformation.Text = rm.GetString("langThisPostInformation");
            langRiceType.Text = rm.GetString("langRiceType");
            langAmount1.Text = rm.GetString("langAmountNotKg");
            langPostTradedAmount.Text = rm.GetString("langPostTradedAmount");
            langUnitPrice1.Text = rm.GetString("langUnitPrice");
            langPostPerPrice.Text = rm.GetString("langPostPerPrice");
            langAddress.Text = rm.GetString("langAddress");
            langProvince.Text = rm.GetString("langProvince");
            btnCancelOrder.OnClientClick = "return confirm('" + rm.GetString("langConfirmCancelOrder") + "');";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (IsPostBack) return;

            // Check User login
            if (Session["userID"] == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_NOT_LOGIN) + "');window.location='../HomePage.aspx'</script>");
            }

            long userID = Convert.ToInt64(Session["userID"]);
            long orderID = Convert.ToInt64(Request.QueryString["orderID"]);

            BOLTrading bolTrading = new BOLTrading();
            DataTable tblOrder = new DataTable();
            DataTable tblPostOrder = new DataTable();
            string message = "";
            long postID = 0;
            short status = 0;
            btnCancelOrder.Enabled = false;

            string lang = Session["language"].ToString();
            if (bolTrading.viewOrder(lang,orderID, out tblOrder, out tblPostOrder, out message))
            {
                if (tblOrder.Rows.Count > 0)
                {
                    // View detail information of order
                    for (int i = 0; i < tblOrder.Rows.Count; ++i)
                    {
                        lblOrderAmount.Text = Convert.ToString(tblOrder.Rows[i]["Amount"]);
                        lblOrderPrice.Text = Convert.ToString(tblOrder.Rows[i]["Price"]);
                        lblOrderDate.Text = Convert.ToString(tblOrder.Rows[i]["UpdateTime"]);
                        lblOrderStatus.Text = Convert.ToString(tblOrder.Rows[i]["StatusName"]);
                        lblOrderStatus.Text = PRLCommon.ChangeLanguageStatus(lblOrderStatus.Text);
                        status = Convert.ToInt16(tblOrder.Rows[i]["Status"]);
                    }

                    if (status == 1)
                    {
                        btnCancelOrder.Enabled = true;
                    }
                }

                if (tblPostOrder.Rows.Count > 0)
                {
                    // View information of post correspond to that order
                    for (int i = 0; i < tblPostOrder.Rows.Count; ++i)
                    {
                        postID = Convert.ToInt64(tblPostOrder.Rows[i]["ID"]);
                        lblPostRice.Text = Convert.ToString(tblPostOrder.Rows[i]["TypeName"]);
                        lblPostAmount.Text = Convert.ToString(tblPostOrder.Rows[i]["Amount"]);
                        lblPostTradedAmount.Text = Convert.ToString(tblPostOrder.Rows[i]["TradedAmount"]);
                        lblPostPrice.Text = Convert.ToString(tblPostOrder.Rows[i]["Price"]);
                        lblPostAddress.Text = Convert.ToString(tblPostOrder.Rows[i]["Address"]);
                        lblPostProvince.Text = Convert.ToString(tblPostOrder.Rows[i]["Province"]);
                        lblPerPrice.Text = Convert.ToString(tblPostOrder.Rows[i]["PercentPrice"]);
                    }
                    Session["postID"] = postID;
                    hplPostDetail.NavigateUrl = "~/PRL.Trading/PostInfoPage.aspx?postID=" + postID.ToString();
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }
        }

        protected void btnCancelOrder_Click(object sender, EventArgs e)
        {
            BOLTrading bolTrading = new BOLTrading();
            string message = "";

            // Show popup confirm

            // Get information of order
            long orderID = Convert.ToInt64(Request.QueryString["orderID"]);

            // Thuc hien delete order

            // If thuc hien success
            if (bolTrading.cancelOrder(orderID, out message))
            {
                // Redirect to TransactionPage
                Response.Redirect("~/PRL.Trading/TransactionPage.aspx");
            }
            // If thuc hien fail
            else
            {
                // Show pop-up message: message & Reload page
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(message) + "');window.location='OrderPage.aspx?orderID=" + orderID + "'</script>");
            }
        }
    }
}